package org.nowcoder.leetcode.hashTable;

import java.util.Arrays;

/**
 * Title  : 128. Longest Consecutive Sequence
 * Source : https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-01
 */
public class LeetCode_128 {
    public int longestConsecutive(int[] nums) {
        //暴力解法: 排序
        if (nums.length <= 1)
            return nums.length;

        Arrays.sort(nums);
        int max = 1, temp = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1] + 1) {
                temp += 1;
            } else {
                if ((nums[i] != nums[i - 1])) {
                    max = Math.max(max, temp);
                    temp = 1;
                }
            }
        }
        return Math.max(max, temp);
    }
}
